﻿#region Usings
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

using BK.BlusinessLogic;
using BK.Model;
using BK.Repositories.Abstract;
using BK.UI.Web.Models;
#endregion

namespace BK.UI.Web.Controllers
{
    public class AccountsController : UserDependedController
    {
        #region Fields
        private readonly AccountsBl _accountsBl;
        private readonly CurrenciesBl _currenciesBl;
        #endregion

        #region C'tors
        public AccountsController(IAccountsRepository accountsRepository,
            ICurrenciesRepository currenciesRepository)
        {
            _accountsBl = new AccountsBl(accountsRepository);
            _currenciesBl = new CurrenciesBl(currenciesRepository);
        }
        #endregion

        #region Instance Methods
        public ActionResult List()
        {
            IEnumerable<Account> model = _accountsBl.GetByUserId(User.Id);
            return View("List", model);
        }
        #endregion

        public ActionResult Edit(int id)
        {
            Account account = _accountsBl.GetById(id);
            
            AccountViewModel model = new AccountViewModel();
            model.Name = account.Name;
            model.Currencies = _currenciesBl.GetByUserId(User.Id);

            return View("EditCreate", model);
        }

        public ActionResult Delete(int id)
        {
            throw new System.NotImplementedException();
        }

        [HttpPost]
        public ActionResult AjaxCurrenciesLoad(string text)
        {
            IEnumerable<Account> accounts = _accountsBl.GetByUserId(User.Id);
            accounts = from a in accounts
                       where a.Name.ToLower().Contains(text.ToLower())
                       select a;
            return new JsonResult
                {
                    Data = new SelectList(accounts.ToList(), "Id", "Name")
                };
        }
    }
}